package com.school_admin.controller;

import com.school_admin.config.Result;
import com.school_admin.entity.Comment;
import com.school_admin.service.impl.CommentServiceImpl;
import com.school_admin.util.CreateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    private final CommentServiceImpl commentService;
    private final CreateTime createTime;

    @Autowired
    public CommentController(CreateTime createTime, CommentServiceImpl commentService) {
        this.createTime = createTime;
        this.commentService = commentService;
    }

    // TODO 发表评论
    @PostMapping("/publish")
    public Result<?> publishComment(Comment comment) {
        comment.setTime(createTime.getTime());
        boolean is = commentService.save(comment);
        if (is) {
            return Result.success("发表评论成功");
        } else {
            return Result.fail("发表评论失败");
        }
    }
    // 获取评论
    @GetMapping("getComment/{postId}")
    public Result<?> getComment(@PathVariable String postId) {
        return commentService.getCommentByPostId(postId);
    }
    // 删除评论
    @DeleteMapping("/deleteComment/{commentId}")
    public Result<?> deleteComment(@PathVariable String commentId) {
        // 删除评论
        boolean is = commentService.removeById(commentId);
        if (is) {
            return Result.success("删除评论成功");
        }
        return Result.fail("删除评论失败");
    }
    // 后台获取评论列表
    @GetMapping("/getCommentList")
    public Result<?> getCommentList(int page,int pageSize,String keyWord) {
        System.out.println("kewWord = " + keyWord);
        if (keyWord == null || keyWord.equals("")) {
            keyWord = "";
            return commentService.getCommentList(page, pageSize);
        } else {
            return commentService.getCommentListByKewWord(page, pageSize, keyWord);
        }
    }
}
